home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Advanced M247288152001.psc / CD_File.bas < prev    next >
Encoding:
BASIC Source File  |  2000-02-02  |  9.1 KB  |  349 lines

  1. Attribute VB_Name = "CD_File"
  2. ' replaces the CommonDialog control.
  3. Option Explicit
  4.  
  5. ' private internal buffer
  6. Dim iAction As Integer
  7. Dim lAPIReturn As Long
  8. Dim bCancelError As Boolean
  9. Dim sDefaultExt As String
  10. Dim sDialogTitle As String
  11. Dim lExtendedError As Long
  12. Dim sFileName As String
  13. Dim sFileTitle As String
  14. Dim sFilter As String
  15. Dim iFilterIndex As Integer
  16. Dim lFlags As Long
  17. Dim lHelpCommand As Long
  18. Dim sHelpContext As String
  19. Dim sHelpFile As String
  20. Dim sHelpKey As String
  21. Dim sInitDir As String
  22. Dim lMax As Long
  23. Dim lMaxFileSize As Long
  24. Dim lMin As Long
  25. Dim objObject As Object
  26.  
  27. Dim lhWndOwner As Long
  28.  
  29. Public Enum DlgFileFlags
  30.    OFN_ALLOWMULTISELECT = &H200
  31.    OFN_CREATEPROMPT = &H2000 = &H80
  32.    OFN_EXPLORER = &H80000
  33.    OFN_EXTENSIONDIFFERENT = &H400
  34.    OFN_FILEMUSTEXIST = &H1000
  35.    OFN_HIDEREADONLY = &H4
  36.    OFN_NameS = &H200000
  37.    OFN_NOCHANGEDIR = &H8
  38.    OFN_NODEREFERENCELINKS = &H100000
  39.    OFN_NONameS = &H40000
  40.    OFN_NONETWORKBUTTON = &H20000
  41.    OFN_NOREADONLYRETURN = &H8000
  42.    OFN_NOTESTFILECREATE = &H10000
  43.    OFN_NOVALIDATE = &H100
  44.    OFN_OVERWRITEPROMPT = &H2
  45.    OFN_PATHMUSTEXIST = &H800
  46.    OFN_READONLY = &H1
  47.    OFN_SHOWHELP = &H10
  48. End Enum
  49.  
  50. 'API
  51. Private Const CLSCD_NOACTION = 0
  52. Private Const CLSCD_SHOWOPEN = 1
  53. Private Const CLSCD_SHOWSAVE = 2
  54. Private Const CLSCD_USERCANCELED = 0
  55. Private Const CLSCD_USERSELECTED = 1
  56.  
  57. Private Const CLSCD_MAXFILESIZE = 128
  58. Private Const CLSCD_ERRNUMUSRCANCEL = 32755
  59. Private Const CLSCD_ERRDESUSRCANCEL = "Cancel was selected."
  60. Private Const CLSCD_ERRNUMUSRBUFFER = 32756
  61. Private Const CLSCD_ERRDESUSRBUFFER = "Buffer to small"
  62.  
  63. Private Const FNERR_BUFFERTOOSMALL = &H3003
  64. Private Const FNERR_FILENAMECODES = &H3000
  65. Private Const FNERR_INVALIDFILENAME = &H3002
  66. Private Const FNERR_SUBCLASSFAILURE = &H3001
  67.  
  68. Private Type tOPENFILENAME
  69.         lStructSize As Long
  70.         hWndOwner As Long
  71.         hInstance As Long
  72.         lpstrFilter As String
  73.         lpstrCustomFilter As String
  74.         nMaxCustFilter As Long
  75.         nFilterIndex As Long
  76.         lpstrFile As String
  77.         nMaxFile As Long
  78.         lpstrFileTitle As String
  79.         nMaxFileTitle As Long
  80.         lpstrInitialDir As String
  81.         lpstrTitle As String
  82.         Flags As DlgFileFlags
  83.         nFileOffset As Integer
  84.         nFileExtension As Integer
  85.         lpstrDefExt As String
  86.         lCustData As Long
  87.         lpfnHook As Long
  88.         lpTemplateName As String
  89. End Type
  90.  
  91. Private Declare Function GetOpenFileNameA Lib "comdlg32.dll" (pOpenfilename As tOPENFILENAME) As Long
  92. Private Declare Function GetSaveFileNameA Lib "comdlg32.dll" (pOpenfilename As tOPENFILENAME) As Long
  93. Private Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
  94.  
  95. ' Read Only
  96. Public Property Get Action() As Integer
  97.    Action = iAction
  98. End Property
  99.  
  100. ' Read Only
  101. Public Property Get APIReturn() As Long
  102.    APIReturn = lAPIReturn
  103. End Property
  104.  
  105. ' Read/Write
  106. Public Property Get CancelError() As Boolean
  107.    CancelError = bCancelError
  108. End Property
  109. Public Property Let CancelError(vNewValue As Boolean)
  110.    bCancelError = vNewValue
  111. End Property
  112.  
  113.  
  114. ' Read/Write
  115. Public Property Get DefaultExt() As String
  116.    DefaultExt = sDefaultExt
  117. End Property
  118. Public Property Let DefaultExt(vNewValue As String)
  119.    sDefaultExt = vNewValue
  120. End Property
  121.  
  122. ' Read/Write
  123. Public Property Get DialogTitle() As String
  124.    DialogTitle = sDialogTitle
  125. End Property
  126. Public Property Let DialogTitle(vNewValue As String)
  127.    sDialogTitle = vNewValue
  128. End Property
  129.  
  130. ' Read Only
  131. Public Property Get ExtendedError() As Long
  132.    ExtendedError = lExtendedError
  133. End Property
  134.  
  135. ' Read/Write
  136. Public Property Get FileName() As String
  137.    FileName = sFileName
  138. End Property
  139. Public Property Let FileName(vNewValue As String)
  140.    sFileName = vNewValue
  141. End Property
  142.  
  143. ' Read/Write
  144. Public Property Get FileTitle() As String
  145.    FileTitle = sFileTitle
  146. End Property
  147. Public Property Let FileTitle(vNewValue As String)
  148.    sFileTitle = vNewValue
  149. End Property
  150.  
  151. ' Read/Write
  152. Public Property Get filter() As String
  153.    filter = sFilter
  154. End Property
  155. Public Property Let filter(vNewValue As String)
  156.    sFilter = vNewValue
  157. End Property
  158.  
  159. ' Read/Write
  160. Public Property Get FilterIndex() As Integer
  161.    FilterIndex = iFilterIndex
  162. End Property
  163. Public Property Let FilterIndex(vNewValue As Integer)
  164.    iFilterIndex = vNewValue
  165. End Property
  166.  
  167. ' Read/Write
  168. Public Property Get Flags() As Long
  169.    Flags = lFlags
  170. End Property
  171. Public Property Let Flags(vNewValue As Long)
  172.    lFlags = vNewValue
  173. End Property
  174.  
  175.  
  176. ' Read/Write
  177. Public Property Get hWndOwner() As Long
  178.    hWndOwner = lhWndOwner
  179. End Property
  180. Public Property Let hWndOwner(vNewValue As Long)
  181.    lhWndOwner = vNewValue
  182. End Property
  183.  
  184. ' Read/Write
  185. Public Property Get HelpCommand() As Long
  186.    HelpCommand = lHelpCommand
  187. End Property
  188. Public Property Let HelpCommand(vNewValue As Long)
  189.    lHelpCommand = vNewValue
  190. End Property
  191.  
  192. ' Read/Write
  193. Public Property Get HelpContext() As String
  194.    HelpContext = sHelpContext
  195. End Property
  196. Public Property Let HelpContext(vNewValue As String)
  197.    sHelpContext = vNewValue
  198. End Property
  199.  
  200. ' Read/Write
  201. Public Property Get HelpFile() As String
  202.    HelpFile = sHelpFile
  203. End Property
  204. Public Property Let HelpFile(vNewValue As String)
  205.    sHelpFile = vNewValue
  206. End Property
  207.  
  208. ' Read/Write
  209. Public Property Get HelpKey() As String
  210.    HelpKey = sHelpKey
  211. End Property
  212. Public Property Let HelpKey(vNewValue As String)
  213.    sHelpKey = vNewValue
  214. End Property
  215.  
  216. ' Read/Write
  217. Public Property Get InitDir() As String
  218.    InitDir = sInitDir
  219. End Property
  220. Public Property Let InitDir(vNewValue As String)
  221.    sInitDir = vNewValue
  222. End Property
  223.  
  224.  
  225. ' Read/Write
  226. Public Property Get MaxFileSize() As Long
  227.    MaxFileSize = lMaxFileSize
  228. End Property
  229. Public Property Let MaxFileSize(vNewValue As Long)
  230.    lMaxFileSize = vNewValue
  231. End Property
  232.  
  233.  
  234. '  Read Only
  235. Public Property Get Object() As Object
  236.    Object = objObject
  237. End Property
  238. 'Provide the ShowOpen method.
  239. Public Sub ShowOpen()
  240.    ShowFileDialog (CLSCD_SHOWOPEN)
  241. End Sub
  242.  
  243. 'Provide the ShowSave method.
  244. Public Sub ShowSave()
  245.    ShowFileDialog (CLSCD_SHOWSAVE)
  246. End Sub
  247.  
  248.  
  249. Private Sub ShowFileDialog(ByVal iAction As Integer)
  250.    Dim vOpenFile As tOPENFILENAME
  251.    Dim lMaxSize As Long
  252.    Dim sFileNameBuff As String
  253.    Dim sFileTitleBuff As String
  254.    
  255.    On Error GoTo ShowFileDialogError
  256.    iAction = iAction  'Action property
  257.    lAPIReturn = 0  'APIReturn property
  258.    lExtendedError = 0  'ExtendedError property
  259.    If lMaxFileSize > 0 Then
  260.       lMaxSize = lMaxFileSize
  261.       Else
  262.       lMaxSize = CLSCD_MAXFILESIZE
  263.       End If
  264.    
  265.    vOpenFile.hWndOwner = lhWndOwner
  266.    vOpenFile.lpstrFile = sFileName & Space(lMaxSize - Len(sFileName) - 1) & vbNullChar
  267.    vOpenFile.nMaxFile = lMaxSize
  268.    vOpenFile.lpstrDefExt = sDefaultExt
  269.    vOpenFile.lpstrFileTitle = Space(lMaxSize - 1) & vbNullChar
  270.    vOpenFile.nMaxFileTitle = lMaxSize
  271.    vOpenFile.lpstrFilter = sAPIFilter(sFilter)
  272.    vOpenFile.nFilterIndex = iFilterIndex
  273.    vOpenFile.Flags = lFlags 'And Not (OFN_ALLOWMULTISELECT)
  274.    vOpenFile.lpstrInitialDir = sInitDir
  275.    vOpenFile.lpstrTitle = sDialogTitle
  276.    vOpenFile.lStructSize = Len(vOpenFile)
  277.    
  278.    Select Case iAction
  279.       Case CLSCD_SHOWOPEN
  280.          lAPIReturn = GetOpenFileNameA(vOpenFile)
  281.       Case CLSCD_SHOWSAVE
  282.          lAPIReturn = GetSaveFileNameA(vOpenFile)
  283.       Case Else   'unknown action
  284.          Exit Sub
  285.    End Select
  286.    
  287.    If lAPIReturn = CLSCD_USERSELECTED Then
  288.       sFileName = sLeftOfNull(vOpenFile.lpstrFile)
  289.       sFileTitle = sLeftOfNull(vOpenFile.lpstrFileTitle)
  290.       Else
  291.       lExtendedError = CommDlgExtendedError
  292.       If lExtendedError = FNERR_BUFFERTOOSMALL Then
  293.          On Error GoTo 0
  294.          err.Raise Number:=CLSCD_ERRNUMUSRBUFFER, Description:=CLSCD_ERRDESUSRBUFFER
  295.          Exit Sub
  296.          Else
  297.          If bCancelError = True Then
  298.             On Error GoTo 0
  299.             err.Raise Number:=CLSCD_ERRNUMUSRCANCEL, Description:=CLSCD_ERRDESUSRCANCEL
  300.             Exit Sub
  301.             End If
  302.          End If
  303.       End If
  304.    Exit Sub
  305.    
  306. ShowFileDialogError:
  307.    Exit Sub
  308.    
  309. End Sub
  310.  
  311. ' commondialog control scheidt de filter underdelen met |
  312. ' api's doen het met chr(0)
  313. ' deze routine zet de control schrijfwijze om in api schrijfwijze
  314. Private Function sAPIFilter(ByVal filter As String) As String
  315.    Dim I As Long
  316.    Dim C As String * 1
  317.    Dim NullFilter As String
  318.    
  319.    For I = 1 To Len(filter)
  320.       C = Mid(filter, I, 1)
  321.       If C = "|" Then
  322.          NullFilter = NullFilter & Chr(0)
  323.          Else
  324.          NullFilter = NullFilter & C
  325.          End If
  326.    Next I
  327.    While Right(NullFilter, 2) <> Chr(0) & Chr(0)
  328.       NullFilter = NullFilter & Chr(0)
  329.    Wend
  330.    sAPIFilter = NullFilter
  331. End Function
  332.  
  333. Private Function sLeftOfNull(ByVal txt As String)
  334.    Dim I As Long, P As Long
  335.    Dim ntxt As String, k As String * 1
  336.       
  337.    P = InStr(txt, Chr(0) & Chr(0))
  338.    If P > 0 Then
  339.       For I = 1 To P - 1
  340.          k = Mid(txt, I, 1)
  341.          If k = Chr(0) Then ntxt = ntxt & " " Else ntxt = ntxt & k
  342.       Next I
  343.       Else
  344.       ntxt = Left(txt, InStr(txt, Chr(0)) - 1)
  345.       End If
  346.    sLeftOfNull = ntxt
  347. End Function
  348.  
  349.